import request from "../../utils/http";
import { UserOrderVo } from "../../utils/type";
import { anyObj, enter } from "../../utils/util";

const USER_STATUS_MAP = {
	0: {
		class: "user-status-pay",
		text: "待支付"
	},
	1: {
		class: "user-status-appointment",
		text: "待预约"
	},
	2: {
		class: "user-status-take-order",
		text: "待接单"
	},
	3: {
		class: "user-status-take-effect",
		text: "待服务"
	},
	4: {
		class: "user-status-ing",
		text: "服务中"
	},
	5: {
		class: "user-status-finished",
		text: "已完成"
	},
	6: {
		class: "user-status-cancel",
		text: "已取消"
	}
};

// pages/userOrderList/userOrderList.ts
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		orderStatus: "null",
		userOrderList: <UserOrderVo[]>[],
		USER_STATUS_MAP
	},

	handleOrderDetail(event: WechatMiniprogram.CustomEvent) {
		enter({
			pageName: 'userOrderDetail',
			query: {
				orderId: event.currentTarget.dataset.orderId
			}
		});
	},

	onTabsChange(event: WechatMiniprogram.CustomEvent) {
		this.setData({
			orderStatus: event.detail.value
		});
		this.getUserOrderList();
	},

	async getUserOrderList() {
		try {
			const query: any = {};
			if (this.data.orderStatus != "null") {
				query.orderStatus = this.data.orderStatus;
			}
			const userOrderList = await request({
				url: '/api/customers/order/getListByStatus',
				method: 'GET',
				query,
				errToast: true,
				showLoading: {}
			});
			this.setData({userOrderList});
		} catch (error) {
			
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(query: anyObj) {
		console.log('query: ', query)
		if (query.status) {
			this.setData({
				orderStatus: query.status
			});
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		console.log('onShow')
		this.getUserOrderList();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})